package com.theoryinpractice.hudson.ui;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.theoryinpractice.hudson.HudsonConfigurationManager;
import com.theoryinpractice.hudson.core.HudsonBuilder;
import com.theoryinpractice.hudson.model.Build;
import com.theoryinpractice.hudson.model.Job;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;
import java.awt.*;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: May 16, 2007
 * Time: 9:00:24 PM
 * To change this template use File | Settings | File Templates.
 */
public class BrowserPanel {
    private static final Logger LOGGER = Logger.getInstance("Hudson");

    private Project project;
    private JPanel panel;
    private JTree tree;

    public BrowserPanel(Project project) {
        this.project = project;

        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.setShowsRootHandles(true);
        tree.setCellRenderer(new JobCellRenderer());

        HudsonUpdater hudsonUpdater = new HudsonUpdater();
        hudsonUpdater.start();
    }


    public JPanel getPanel() {
        return panel;
    }

    private static class JobCellRenderer extends JLabel implements TreeCellRenderer {
        public Component getTreeCellRendererComponent(JTree jTree, Object o, boolean b, boolean b1, boolean b2, int i, boolean b3) {

            DefaultMutableTreeNode node = (DefaultMutableTreeNode) o;

            Object object = node.getUserObject();

            if (object instanceof String) {
                setText((String) object);
                setIcon(null);
            } else if (object instanceof Job) {

                Job job = (Job) node.getUserObject();

                if ("blue".equals(job.getColor())) {
                    setIcon(IconLoader.getIcon("/general/errorsFound.png"));
                } else {
                    setIcon(IconLoader.getIcon("/general/errorsOK.png"));
                }

                setText(job.getName());
            } else if (object instanceof Build) {
                Build build = (Build) node.getUserObject();
                setText(build.toString());
            }
            return this;
        }

    }

    private void updateServers() {
        LOGGER.info("Refreshing Hudson Servers");

        HudsonConfigurationManager hudsonConfigurationManager = HudsonConfigurationManager.getInstance();
        if (hudsonConfigurationManager.getState().hudsonServers.isEmpty()) {
            hudsonConfigurationManager.getState().hudsonServers.add("http://localhost:8080");
        }


        LOGGER.info("Hudson Configuration Manager is: " + hudsonConfigurationManager);
        HudsonBuilder hudsonBuilder = new HudsonBuilder();

        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Hudson");

        HudsonConfigurationManager.ConfigurationBean configuration = hudsonConfigurationManager.getState();
        Set<String> servers = configuration.hudsonServers;
        for (String server : servers) {
            LOGGER.info("Refreshing server " + server);
            DefaultMutableTreeNode node = hudsonBuilder.buildHudsonTreeNode(server);
            rootNode.add(node);
        }

        tree.setModel(new DefaultTreeModel(rootNode));
    }

    private class HudsonUpdater extends Thread {

        private boolean terminate;

        public void terminate() {
            terminate = true;
        }

        public void run() {
            try {
                while (!terminate) {
                    updateServers();
                    sleep(60000);
                }
            } catch (InterruptedException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    }
}
